import os
import sys
import xlrd
import django
from xlrd import xldate_as_tuple

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()

from apps.supply_order.models import YF
from apps.supply_order.models import GoodsType
from core.tools.models import Province


def init_data(xlsx, goods_type, sz):
    """
    :param xlsx: 文件
    :param goods_type:商品类型
    :param sz: 首重
    :return:
    """
    data = xlrd.open_workbook(xlsx)  # 打开excel
    table = data.sheets()[0]
    rows = table.nrows  # 行数
    goods_type = GoodsType.objects.get(goods_type=goods_type)
    yf_obj_list = []
    for i in range(1, rows):
        row_values = table.row_values(i)  # 某一行数据
        province_name = row_values[0]
        province = Province.objects.get(name__contains=province_name)
        sz_price = row_values[1] * 100
        xz_price = row_values[2] * 100
        yf_obj = YF(sz_price=sz_price, xz_price=xz_price, goods_type=goods_type, province_name=province_name, sz=sz,
                    province=province)
        yf_obj_list.append(yf_obj)
    YF.objects.bulk_create(yf_obj_list)


def create_data():
    xlsx = "yunfei.xls"
    goods_type = 142
    sz = 5
    init_data(xlsx, goods_type, sz)


if __name__ == '__main__':
    create_data()
